﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MoLingOverhaul.Common.Cores.Data;
using MoLingOverhaul.Common.Cores.Enum;
using MoLingOverhaul.Common.Helpers;
using MoLingOverhaul.Common.Systems;
using ReLogic.Graphics;
using Terraria;
using Terraria.GameContent;
using Terraria.Localization;
using Terraria.UI;

namespace MoLingOverhaul.Common.UI
{
    public class SeasonUI : UIState
    {
        private const int PanelWidth = 130;
        private const int PanelHeight = 60;
        private const int ProgressBarHeight = 8;
        private static Texture2D SpringIcon => TextureHelper.GetTexture("Spring");
        private static Texture2D SummerIcon => TextureHelper.GetTexture("Summer");
        private static Texture2D AutumnIcon => TextureHelper.GetTexture("Autumn");
        private static Texture2D WinterIcon => TextureHelper.GetTexture("Winter");

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (SeasonSystem.Instance.CurrentSeason != null)
            {
                var seasonData = SeasonSystem.Instance.CurrentSeason;
                Vector2 panelPosition = new(Main.screenWidth - PanelWidth - 340, 60);
                DrawMainPanel(spriteBatch, panelPosition, seasonData);
                DrawProgressBar(spriteBatch, panelPosition + new Vector2(0, PanelHeight + 5), seasonData);
                DrawTooltip(spriteBatch, panelPosition, seasonData);
            }
        }
        private static void DrawMainPanel(SpriteBatch spriteBatch, Vector2 position, SeasonData seasonData)
        {
            Rectangle panelRect = new((int)position.X, (int)position.Y, PanelWidth, PanelHeight);
            DrawPanelBackground(spriteBatch, panelRect);
            Texture2D icon = GetSeasonIcon(seasonData.CurrentSeason);
            Rectangle iconRect = new((int)position.X + 10, (int)position.Y + 10, 18, 24);
            if (icon != null)
            {
                spriteBatch.Draw(icon, iconRect, Color.White * 0.9f);
            }
            string seasonText = seasonData.GetSeasonName();
            string dayText = Language.GetTextValue("Mods.MoLingOverhaul.UI.1") + $"{seasonData.DaysInCurrentSeason + 1}";
            Vector2 textPosition = position + new Vector2(55, 10);
            Color seasonColor = GetSeasonColor(seasonData.CurrentSeason);
            Utils.DrawBorderString(spriteBatch, seasonText, textPosition, seasonColor, 0.9f);
            Vector2 dayPosition = textPosition + new Vector2(0, 20);
            Utils.DrawBorderString(spriteBatch, dayText, dayPosition, Color.LightGray, 0.8f);
        }
        private static void DrawProgressBar(SpriteBatch spriteBatch, Vector2 position, SeasonData seasonData)
        {
            const int barWidth = PanelWidth;
            const int barHeight = ProgressBarHeight;
            Rectangle backgroundRect = new((int)position.X, (int)position.Y, barWidth, barHeight);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, backgroundRect, Color.Gray * 0.3f);
            float progress = seasonData.SeasonProgress;
            Rectangle progressRect = new((int)position.X, (int)position.Y, (int)(barWidth * progress), barHeight);
            Color progressColor = GetSeasonColor(seasonData.CurrentSeason);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, progressRect, progressColor);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle((int)position.X, (int)position.Y, barWidth, 1), Color.Black * 0.8f);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle((int)position.X, (int)position.Y + barHeight - 1, barWidth, 1), Color.Black * 0.8f);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle((int)position.X, (int)position.Y, 1, barHeight), Color.Black * 0.8f);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle((int)position.X + barWidth - 1, (int)position.Y, 1, barHeight), Color.Black * 0.8f);
        }
        private static void DrawTooltip(SpriteBatch spriteBatch, Vector2 panelPosition, SeasonData seasonData)
        {
            Rectangle panelArea = new((int)panelPosition.X, (int)panelPosition.Y, PanelWidth, PanelHeight + ProgressBarHeight + 10);
            if (panelArea.Contains(Main.MouseScreen.ToPoint()))
            {
                string tooltipText = GetSeasonEffects(seasonData.CurrentSeason);
                Vector2 tooltipPos = Main.MouseScreen + new Vector2(20, 20);
                DynamicSpriteFont font = FontAssets.MouseText.Value;
                Vector2 textSize = font.MeasureString(tooltipText);
                int borderThickness = 3;
                Rectangle tooltipRect = new((int)tooltipPos.X - 5 - borderThickness, (int)tooltipPos.Y - 5 - borderThickness, (int)textSize.X + 10 + borderThickness * 2, (int)textSize.Y + 10 + borderThickness * 2);
                Color borderColor = GetSeasonColor(seasonData.CurrentSeason);
                spriteBatch.Draw(TextureAssets.MagicPixel.Value, tooltipRect, borderColor);
                Rectangle backgroundRect = new(tooltipRect.X + borderThickness, tooltipRect.Y + borderThickness, tooltipRect.Width - borderThickness * 2, tooltipRect.Height - borderThickness * 2);
                spriteBatch.Draw(TextureAssets.MagicPixel.Value, backgroundRect, Color.Black * 0.9f);
                Vector2 textPos = new(backgroundRect.X + 5, backgroundRect.Y + 5);
                Utils.DrawBorderString(spriteBatch, tooltipText, textPos, Color.White, 0.8f);
            }
        }

        private static void DrawPanelBackground(SpriteBatch spriteBatch, Rectangle rect)
        {
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, rect, Color.Black * 0.7f);
            Color borderColor = GetSeasonColor(SeasonSystem.Instance.CurrentSeason.CurrentSeason) * 0.8f;
            int borderThickness = 4;
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(rect.X, rect.Y, rect.Width, borderThickness), borderColor);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(rect.X, rect.Y + rect.Height - borderThickness, rect.Width, borderThickness), borderColor);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(rect.X, rect.Y, 6, rect.Height), borderColor);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(rect.X + rect.Width - borderThickness, rect.Y, borderThickness, rect.Height), borderColor);
        }
        private static Texture2D GetSeasonIcon(Season season)
        {
            return season switch
            {
                Season.Spring => SpringIcon,
                Season.Summer => SummerIcon,
                Season.Autumn => AutumnIcon,
                Season.Winter => WinterIcon,
                _ => null
            };
        }

        private static Color GetSeasonColor(Season season)
        {
            return season switch
            {
                Season.Spring => new Color(120, 200, 80),
                Season.Summer => new Color(255, 140, 0),
                Season.Autumn => new Color(180, 100, 50),
                Season.Winter => new Color(150, 200, 255),
                _ => Color.White
            };
        }

        private static string GetSeasonEffects(Season season)
        {
            return season switch
            {
                Season.Spring => Language.GetTextValue("Mods.MoLingOverhaul.UI.2"),
                Season.Summer => Language.GetTextValue("Mods.MoLingOverhaul.UI.3"),
                Season.Autumn => Language.GetTextValue("Mods.MoLingOverhaul.UI.4"),
                Season.Winter => Language.GetTextValue("Mods.MoLingOverhaul.UI.5"),
                _ => Language.GetTextValue("Mods.MoLingOverhaul.UI.6")
            };
        }
    }
}